/**
 * Created by xuhenian on 2016/8/31.
 */
var gulp = require('gulp'),
  rename = require('gulp-rename'),
  // ts = require('gulp-typescript'),
  sourcemaps = require('gulp-sourcemaps'),
  babel = require('gulp-babel'),
  del = require('del');

//**-- 配置 --**//
var
  src = {
    pageVM: './src/page/**/*.html',
    widgetVM: './src/widget/**/*.html',
    script: './src/script/**/*.js',
    scriptLibs: './src/script/libs/**/*.js'
  },
  dest = {
    pageVM: './page',
    widgetVM: './widget',
    script: './static/script',
    scriptLibs: './static/script/libs'
  },
  config = {
    map: true
  };

//*****************************-- 构建 --*****************************//

//构建 page/*.vm
gulp.task('B:page.vm', function () {
  gulp.src(src.pageVM)
    .pipe(rename(function (path) {
      path.extname = '.vm';
    }))
    .pipe(gulp.dest(dest.pageVM));
});
//构建 widget/*.vm
gulp.task('B:widget.vm', function () {
  gulp.src(src.widgetVM)
    .pipe(rename(function (path) {
      path.extname = '.vm';
    }))
    .pipe(gulp.dest(dest.widgetVM));
});
//构建 script
gulp.task('B:script', function () {
  var jsFile = gulp.src([src.script, '!' + src.scriptLibs]);
  config.map && jsFile.pipe(sourcemaps.init());
  jsFile.pipe(babel());
  config.map && jsFile.pipe(sourcemaps.write());
  jsFile.pipe(gulp.dest(dest.script));


  gulp.src(src.scriptLibs).pipe(gulp.dest(dest.scriptLibs));
});

//*****************************-- 清理 --*****************************//

//清除 page/*.vm
gulp.task('C:page.vm', function (cb) {
  del(dest.pageVM + '/**/*.vm', cb);
});
//清除 widget/*.vm
gulp.task('C:widget.vm', function (cb) {
  del(dest.widgetVM + '/**/*.vm', cb);
});
//清除 script
gulp.task('C:script', function (cb) {
  del(dest.script + '/**/*.js', cb);
});
//*****************************-- 整合 --*****************************//

//构建所有
gulp.task('B:all', ['B:page.vm', 'B:widget.vm', 'B:script']);
//清除所有
gulp.task('C:all', ['C:page.vm', 'C:widget.vm', 'C:script']);

//*****************************-- 监听 --*****************************//

//监听所有
gulp.task('W:all', function () {
  gulp.watch([src.pageVM], ['B:page.vm']);
  gulp.watch([src.widgetVM], ['B:widget.vm']);
  gulp.watch([src.script], ['B:script']);
});
